package com.kantboot.functional.pay.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kantboot.functional.pay.order.domian.dto.FunctionPayOrderDTOOfOrderPaidAfter;
import com.kantboot.functional.pay.order.domian.dto.GeneratePayOrderDTO;
import com.kantboot.functional.pay.order.domian.entity.FunctionalPayOrder;
import com.kantboot.functional.pay.order.exception.FunctionalPayOrderException;
import com.kantboot.functional.pay.order.repository.FunctionalPayOrderRepository;
import com.kantboot.functional.pay.order.service.IFunctionalPayOrderService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.common.exception.BaseException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付订单服务
 */
@Service
public class FunctionalPayOrderServiceImpl implements IFunctionalPayOrderService {

    @Resource
    private FunctionalPayOrderRepository repository;

    @Resource
    private IUserSelfService service;

    @Override
    public FunctionalPayOrder generatePayOrder(GeneratePayOrderDTO dto) {
        FunctionalPayOrder payOrder = BeanUtil.copyProperties(dto, FunctionalPayOrder.class);
        // 清除id，防止存入数据库时id冲突
        payOrder.setId(null);

        // 设置发起人的用户id
        if(dto.getUserId() != null){
            // 如果传入了用户id，就使用传入的用户id
            payOrder.setUserId(dto.getUserId());
        }else{
            // 如果没有传入用户id，就使用当前登录用户的id
            Long selfId = service.getSelfId();
            payOrder.setUserId(selfId);
        }


        // 设置成未支付状态
        payOrder.setStatusCode(FunctionalPayOrder.STATUS_CODE_UNPAID);

        // TODO 之后自己的项目打开
        // 设置成为状态处理
//        payOrder.setIsStatusProcessed(false);
        payOrder.setIsStatusProcessed(true);

        // TODO 之后自己的项目打开
        // 设置成为支付处理
//        payOrder.setIsPaidAfterCheckProcessed(false);
        payOrder.setIsPaidAfterCheckProcessed(true);

        // TODO 之后删掉
        payOrder.setIsPaidAfterCheckSuccess(true);
        return repository.save(payOrder);
    }

    @Override
    public FunctionalPayOrder getById(Long id) {
        return repository.findById(id).orElseThrow(() ->
                        // 如果支付订单不存在，抛出异常
                        FunctionalPayOrderException.PAY_ORDER_NOT_FOUND
                );
    }

    @Override
    public FunctionalPayOrder getUnpaidById(Long id) {
        FunctionalPayOrder functionalPayOrder = getById(id);
        if(FunctionalPayOrder.STATUS_CODE_UNPAID.equals(functionalPayOrder.getStatusCode())){
            return functionalPayOrder;
        }
        // 如果不是未支付状态，抛出异常
        throw FunctionalPayOrderException.PAY_ORDER_NOT_UNPAID;
    }

    @Override
    public void updateStatusCodeById(Long id, String statusCode) {
        repository.updateStatusCodeById(id, statusCode);
    }

    @Override
    public void updatePayMethodCodeById(Long id, String payMethodCode) {
        repository.updatePayMethodCodeById(id, payMethodCode);
    }
    @Override
    public void handlePaidAfter(FunctionPayOrderDTOOfOrderPaidAfter dto){
        FunctionalPayOrder functionalPayOrder = getById(dto.getId());
        // 设置成为支付处理设置为true
        functionalPayOrder.setIsPaidAfterCheckProcessed(true);
        functionalPayOrder.setIsPaidAfterCheckSuccess(dto.getIsPaidAfterCheckSuccess());
        functionalPayOrder.setPaidAfterCheckFailReasonCode(dto.getPaidAfterCheckFailReasonCode());
        functionalPayOrder.setPaidAfterCheckFailReasonDescription(dto.getPaidAfterCheckFailReasonDescription());
        repository.save(functionalPayOrder);
    }

    @Override
    public Boolean checkPaidAfterSuccess(Long id) {
        FunctionalPayOrder order = getById(id);
        if(!order.getIsPaidAfterCheckProcessed()){
            // 如果没有支付处理过，返回false
            return false;
        }

        if(!order.getIsPaidAfterCheckSuccess()){
            throw BaseException.of(order.getPaidAfterCheckFailReasonCode(),order.getPaidAfterCheckFailReasonDescription());
        }

        return true;
    }

    @Override
    public List<FunctionalPayOrder> queryUnPayOrder() {
        return repository.findByStatusCode(FunctionalPayOrder.STATUS_CODE_UNPAID);
    }


}








